package com.citcc.api.utils;

import com.citcc.business.bean.dto.PayDTO;
import com.citcc.business.util.IdGenerator;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;

public class PaySystemUtil {

    //微信参数配置
    public static String API_KEY = "bubugao5031301455500613014555006";
    public static String APPID = "wx6e542f1a6a4a2d39";
    public static String MCH_ID = "1532090131";
    /**
     * @param payDTO
     * @param request
     * @param attachmap
     * @return
     */
    @SuppressWarnings("unchecked")
    public static Map<String, String> WeiXinPrePay(PayDTO payDTO, HttpServletRequest request, String attachmap) {

        System.out.println("------------封装");
        SortedMap<String, Object> parameterMap = new TreeMap<String, Object>();
        parameterMap.put("appid", payDTO.getAppid());
        parameterMap.put("mch_id", payDTO.getMch_id());
        parameterMap.put("nonce_str", IdGenerator.generateId());
        parameterMap.put("body", payDTO.getDescription());
        parameterMap.put("attach", attachmap);
        parameterMap.put("out_trade_no", payDTO.getOut_trade_no());
        parameterMap.put("fee_type", "CNY");
        BigDecimal total = payDTO.getPaymoney().multiply(new BigDecimal(100));
        java.text.DecimalFormat df = new java.text.DecimalFormat("0");
        parameterMap.put("total_fee", df.format(total));
        parameterMap.put("spbill_create_ip", request.getRemoteAddr());
        parameterMap.put("notify_url", payDTO.getWxnotifyURL());
        parameterMap.put("trade_type", "APP");
        //trade_type为JSAPI是 openid为必填项
        // parameterMap.put("openid", openid);
        String sign = WeiXinPayUtil.createSign("UTF-8", parameterMap);
        parameterMap.put("sign", sign);
        Map<String, String> map = null;
        try {
            String requestXML = WeiXinPayUtil.mapToXml(parameterMap);
            System.out.println(requestXML);
            String result = WeiXinPayUtil.httpsRequest(
                    "https://api.mch.weixin.qq.com/pay/unifiedorder", "POST",
                    requestXML);
            System.out.println(result);
            map = WeiXinPayUtil.xmlToMap(result);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return map;
    }

    /**
     * @param payDTO
     * @param request
     * @param attachmap
     * @return
     */
    @SuppressWarnings("unchecked")
    public static Map<String, String> WeiXinXcxPrePay(PayDTO payDTO, HttpServletRequest request, String attachmap, String openid) {

        System.out.println("------------封装");
        SortedMap<String, Object> parameterMap = new TreeMap<String, Object>();
        parameterMap.put("appid", payDTO.getAppid());
        parameterMap.put("mch_id", payDTO.getMch_id());
        parameterMap.put("nonce_str", IdGenerator.generateId());
        parameterMap.put("body", payDTO.getDescription());
        parameterMap.put("attach", attachmap);
        parameterMap.put("out_trade_no", payDTO.getOut_trade_no());
        parameterMap.put("fee_type", "CNY");
        BigDecimal total = payDTO.getPaymoney().multiply(new BigDecimal(100));
        java.text.DecimalFormat df = new java.text.DecimalFormat("0");
        parameterMap.put("total_fee", df.format(total));
        parameterMap.put("spbill_create_ip", request.getRemoteAddr());
        parameterMap.put("notify_url", payDTO.getWxnotifyURL());
        parameterMap.put("trade_type", "JSAPI");
        //trade_type为JSAPI是 openid为必填项
        parameterMap.put("openid", openid);
        String sign = WeiXinPayUtil.createSign("UTF-8", parameterMap);
        parameterMap.put("sign", sign);
        Map<String, String> map = null;
        try {
            String requestXML = WeiXinPayUtil.mapToXml(parameterMap);
            System.out.println(requestXML);
            String result = WeiXinPayUtil.httpsRequest(
                    "https://api.mch.weixin.qq.com/pay/unifiedorder", "POST",
                    requestXML);
            System.out.println(result);
            map = WeiXinPayUtil.xmlToMap(result);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return map;
    }
    /**
     * @param payDTO
     * @param request
     * @param attachmap
     * @return
     */
    @SuppressWarnings("unchecked")
    public static Map<String, String> WeiXinXcxPrePayCZT(PayDTO payDTO, HttpServletRequest request, String attachmap, String openid) {

        System.out.println("------------封装");
        SortedMap<String, Object> parameterMap = new TreeMap<String, Object>();
        parameterMap.put("appid", payDTO.getAppid());
        parameterMap.put("mch_id", payDTO.getMch_id());
        parameterMap.put("nonce_str", IdGenerator.generateId());
        parameterMap.put("body", payDTO.getDescription());
        parameterMap.put("attach", attachmap);
        parameterMap.put("out_trade_no", payDTO.getOut_trade_no());
        parameterMap.put("fee_type", "CNY");
        BigDecimal total = payDTO.getPaymoney().multiply(new BigDecimal(100));
        java.text.DecimalFormat df = new java.text.DecimalFormat("0");
        parameterMap.put("total_fee", df.format(total));
        parameterMap.put("spbill_create_ip", request.getRemoteAddr());
        parameterMap.put("notify_url", payDTO.getWxnotifyURL());
        parameterMap.put("trade_type", "JSAPI");
        //trade_type为JSAPI是 openid为必填项
        parameterMap.put("openid", openid);
        String sign = WeiXinPayUtil.createSignCZT("UTF-8", parameterMap);
        parameterMap.put("sign", sign);
        Map<String, String> map = null;
        try {
            String requestXML = WeiXinPayUtil.mapToXml(parameterMap);
            System.out.println(requestXML);
            String result = WeiXinPayUtil.httpsRequest(
                    "https://api.mch.weixin.qq.com/pay/unifiedorder", "POST",
                    requestXML);
            System.out.println(result);
            map = WeiXinPayUtil.xmlToMap(result);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return map;
    }

    public static void main(String[] args) {
        BigDecimal feeNo = new BigDecimal(1);//string 转 BigDecimal 分转元
        //feeNo = feeNo.divide(new BigDecimal(100), 2, RoundingMode.HALF_UP);
        String string = feeNo.multiply(new BigDecimal(100)).toString();
        System.out.println(string);
    }

    /**
     * @param payDTO
     * @param attachmap
     * @return
     */
    @SuppressWarnings("unchecked")
    public static Map<String, String> officePayWX(PayDTO payDTO, String attachmap, String openid, String ip) {

        System.out.println("------------封装");
        SortedMap<String, Object> parameterMap = new TreeMap<String, Object>();
        parameterMap.put("mch_appid", APPID);
        parameterMap.put("mchid", MCH_ID);
        parameterMap.put("nonce_str", IdGenerator.generateId());

        parameterMap.put("partner_trade_no", payDTO.getOut_trade_no());
        parameterMap.put("openid", openid);

        parameterMap.put("check_name", "NO_CHECK");
        parameterMap.put("amount", payDTO.getPaymoney());
        parameterMap.put("desc", payDTO.getDescription());
        BigDecimal feeNo = payDTO.getPaymoney();//string 转 BigDecimal 分转元
        feeNo = feeNo.multiply(new BigDecimal(100));//分转元
        parameterMap.put("amount", feeNo.intValue() + "");
        //parameterMap.put("attach", attachmap);
        parameterMap.put("spbill_create_ip", ip);

        String sign = WeiXinPayUtil.createSign("UTF-8", parameterMap);
        parameterMap.put("sign", sign);
        Map<String, String> map = null;
        try {
            String requestXML = WeiXinPayUtil.getRequestXml(parameterMap);
            System.out.println(requestXML);
            String result = WeiXinPayUtil.httpsRequest2(
                    "https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers", "POST",
                    requestXML);
            System.out.println(result);
            map = WeiXinPayUtil.xmlToMap(result);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return map;
    }



}
